<template>
  <a-modal :title="this.title" :visible="this.visible" @cancel="cancel" @ok="ok">
    <a-form :form="form" layout="horizontal">
      <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="目录名称">
        <a-input v-decorator="this.validator.menuName" />
      </a-form-item>
      <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="目录描述">
        <a-input type="textarea" v-decorator="this.validator.discription" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import aModal from '@/common/modal';
import { addMenuForm } from '@/../static/js/formValidators.js';
import { ADDMENU } from '@/../static/js/apis.js';

export default {
  inject:['reload'],                                 //注入App里的reload方法
  name: 'addMenuModal',
  props: ['title', 'visible', 'value','type'],
  components: {
    aModal
  },
  data () {
    return {
      form: this.$form.createForm(this),
      labelCol: { span: 5 },
      wrapperCol: { span: 18 },
      /* 验证规则 */
      validator: addMenuForm
    }
  },
  //  watch: {
  //     type(value) {
  //       console.log(value);
  //     },
  // },
  methods: {
    ok (e) {
      const self = this
      e.preventDefault()
      self.form.validateFields((err, values) => {
        if (!err) {
          if(self.type=='下级菜单'){
              values.pid = this.value
              values.htmlUrl='/newPage'
              console.log('Received values of form: ', values)
              ADDMENU(values, function (res) {
                if (res.code='200') {
                  self.$message.success(res.message)
                  self.$emit('ok', e)
                  self.reload()
                  // self.$router.push({path: '/newPage',query:{system:'导航图数据管理软件',menuId:res.obj.id}})
                  self.$store.commit('changeFirstNavSelected', res.obj.key)
                } else self.$message.error(res.message)
              })
          }else if(self.type=='一级菜单'){
              values.pid =0
              values.htmlUrl=null
              values.icon="down-square"
             ADDMENU(values, function (res) {
                if (res.code='200') {
                  self.$message.success(res.message)
                  self.$emit('ok', e)
                  self.reload()
                } else self.$message.error(res.message)
              })
          }

        } else {
          self.$message.error('请按要求填写目录信息！')
        }
      })
    },
    cancel (e) {
      this.form.resetFields()
      this.$emit('cancel', e)
    }
  }
}
</script>

<style scoped>
.ant-form-item {
  margin-bottom: 0px;
}
</style>
